package com.hsyt.pms.controller.base;

import com.hsyt.pms.api.constants.CommonConstants;
import com.hsyt.pms.api.entity.system.PfAdmin;
import com.hsyt.pms.api.entity.system.SysUser;
import com.hsyt.pms.api.vo.system.SysDictionaryVo;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

public class BaseController {

	private final  static Logger LOGGER = LoggerFactory.getLogger(BaseController.class);

	protected PfAdmin getSessionUser(HttpServletRequest request) {
		Object obj = request.getSession().getAttribute("sessionUser");
		if (obj != null){
			return (PfAdmin) obj;
		}
		return null;
	}

	@ModelAttribute
    public void commonModel(Model model, HttpServletRequest request, HttpServletResponse response) {
		// 上下文路径：'/projectName'
		String contextPath = request.getContextPath();
		model.addAttribute("contextPath", contextPath);
		// 全文路径：'http://localhost:8080/projectName'
		String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+contextPath;
		model.addAttribute("basePath", basePath);
		model.addAttribute("staticVersion", "");
		model.addAttribute("staticUrl", "");
	}

	protected SysUser getSessionPmsUser(HttpServletRequest request) {
		Object obj = request.getSession().getAttribute(CommonConstants.SESSION_USER_KEY);
		if (obj != null) {
			return (SysUser) obj;
		}
		return null;
	}

	public static String getComboDataFromDictList(List<SysDictionaryVo> sysDictList, String selectValue) {
		StringBuilder resultStr = new StringBuilder("([[\'" + selectValue + "\', \'--选择--\'],");
		if (CollectionUtils.isEmpty(sysDictList)) {
			return "([[\'" + selectValue + "\', \'--选择--\']])";
		} else {
			for (int i = 0; i < sysDictList.size(); i++) {
				SysDictionaryVo dto = sysDictList.get(i);
				resultStr.append("[\'");
				resultStr.append(dto.getDictCode());
				resultStr.append("\',\'");
				resultStr.append(dto.getDictName());
				resultStr.append("\']");
				if (i != sysDictList.size() - 1) {
					resultStr.append(",");
				}
			}
			resultStr.append("])");
		}
		return resultStr.toString();
	}

	public static String getComboData(List<SysDictionaryVo> sysDictList, String selectValue) {
		StringBuilder returnStr = new StringBuilder("([[\'").append(selectValue).append("\', \'--请选择--\'],");
		if (sysDictList == null || sysDictList.isEmpty()) {
			return "([[\'" + selectValue + "\', \'--请选择--\']])";
		} else {
			for (int i = 0; i < sysDictList.size(); i++) {
				SysDictionaryVo dto = sysDictList.get(i);
				returnStr.append("[\'");
				returnStr.append(dto.getDictCode());
				returnStr.append("\',\'");
				returnStr.append(dto.getDictName());
				returnStr.append("\']");
				if (i != sysDictList.size() - 1) {
					returnStr.append(",");
				}
			}
			returnStr.append("])");
		}
		return returnStr.toString();
	}

}
